package manager; 
 
import java.util.Date; 
import java.util.Properties; 
import javax.ejb.EJBException; 
import javax.mail.Message; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import util.SMTPAuthenticator; 
 
 
public class EmailManager  
{ 
String emailServerName = "mailauth.comp.nus.edu.sg"; 
// Replace with your real name and unix email address         
String emailFromAddress = "Merlion's System Auto Generated Email<pnhv88@comp.nus.edu.sg>"; 
// Replace with your real name and unix email address     
    String toEmailAddress;  
    String mailer = "JavaMailer"; 
 
         
         
    public EmailManager() 
    { 
    } 
     
    public void setEmailAddress(String name, String email){
        this.toEmailAddress = name + "<" + email + ">";
    }
    
    public void sentEmail(String subject, String content) 
    { 
         
        try  
        { 
            Properties props = new Properties(); 
            props.put("mail.transport.protocol", "smtp"); 
            props.put("mail.smtp.host", emailServerName); 
            props.put("mail.smtp.port", "25"); 
            props.put("mail.smtp.auth", "true"); 
            props.put("mail.smtp.starttls.enable", "true"); 
            props.put("mail.smtp.debug", "true");             
            javax.mail.Authenticator auth = new SMTPAuthenticator(); 
            Session session = Session.getInstance(props, auth); 
            session.setDebug(true);             
            Message msg = new MimeMessage(session); 
                                     
            if (msg != null) 
            { 
                msg.setFrom(InternetAddress.parse(emailFromAddress, false)[0]); 
                msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmailAddress, false)); 
                msg.setSubject(subject);                 
                msg.setText(content); 
                msg.setHeader("X-Mailer", mailer); 
                 
                Date timeStamp = new Date(); 
                msg.setSentDate(timeStamp); 
                 
                Transport.send(msg);                                                
            }                         
        } 
        catch (Exception e)  
        { 
            e.printStackTrace(); 
             
            throw new EJBException(e.getMessage());                         
        } 
    } 
}